// 根据经纬度获取距离
function getDistance(lon1, lat1, lon2, lat2,) {
	// export function getDistance(lon1, lat1, lon2, lat2,) {
	var radLat1 = lat1 * Math.PI / 180.0;
	var radLat2 = lat2 * Math.PI / 180.0;
	var a = radLat1 - radLat2;
	var b = lon1 * Math.PI / 180.0 - lon2 * Math.PI / 180.0;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	return s;
}
// 设置公司距离
function setCentersList(list, lon, lat) {
	list.forEach(item => {
		let distance = getDistance(
			item.longitude,
			item.latitude,
			lon,
			lat
		)
		item.distance = distance.toFixed(1)
	})
	return list.sort((a, b) => a.distance - b.distance)
}
export { getDistance, setCentersList }

// 处理富文本图片
export function formatRichText(html) {
	let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
		match = match
			.replace(/style="[^"]+"/gi, '')
			.replace(/style='[^']+'/gi, '')
		// match = match
		//   .replace(/width="[^"]+"/gi, '')
		//   .replace(/width='[^']+'/gi, '')
		match = match
			.replace(/height="[^"]+"/gi, '')
			.replace(/height='[^']+'/gi, '')
		return match
	})
	newContent = newContent.replace(/style="[^"]+"/gi, function (
		match,
		capture
	) {
		match = match
			.replace(/width:[^;]+;/gi, 'max-width:100%;')
			.replace(/width:[^;]+;/gi, 'max-width:100%;')
		return match
	})
	newContent = newContent.replace(/<br[^>]*\/>/gi, '')
	newContent = newContent.replace(
		/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin-top:0;margin-bottom:0;"'
	)
	return newContent;
}

export function getTiem(time) {
	let d1 = new Date(time)
	let d2 = new Date()
	let cha = Math.abs(d2.getTime() - d1.getTime())
	let days = parseInt(cha / (24 * 60 * 60 * 1000))
	let hours = parseInt((cha % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000))
	let mins = parseInt((cha % (60 * 60 * 1000)) / (60 * 1000))
	let msec = parseInt(cha % 1000 % 60)
	let arr = []
	arr[0] = days < 10 ? `0${days}` : days
	arr[1] = hours < 10 ? `0${hours}` : hours
	arr[2] = mins < 10 ? `0${mins}` : mins
	arr[3] = msec < 10 ? `0${msec}` : msec
	return arr
}
// 转换参数
export function changeUrl(params) {
	let obj = {}
	let parr = params.split('&')
	for (let i of parr) {
		// 遍历数组
		let arr = i.split('=')
		obj[arr[0]] = arr[1]
	}
	return obj
}
// 链接拼接
export let wx = '/wx'
// export let wx = '/demo/wx'
export let joinUrl = '/wxapp/api/group'
export let joinUrl_audi = '/wxapp/api/audi'
// export let joinUrl = '/demo/wxapp/api/group'